package com.tepth.maintenancesystem.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by LHQ on 2016/12/7.
 */

public class HttpClient {

//    public static final String BASE_URL = "http://dec.tepth.cn/vm/";
public static final String BASE_URL = "http://192.168.1.11:8091/";//http://192.168.1.11:8091/
    //http://192.168.1.11:8095/verification.ashx?platform=SmartFam&action=Login&loginName=ltsby4z&password=ltsby4z&appName=smartFAM_apk&appVersion=1.0&osName=Android&osVersion=6.0
    //http://192.168.1.11:8095/vm/maintenance.ashx?platform=SmartFam&action=GetList&authKey=f28fa49fbc404e128bb5c41113bf8fb4&startTime=2017-01-15&endTime=2017-01-16
    //http://192.168.1.11:8095/vm/Maintenance.ashx?platform=SmartFam&action=UpdateDriverAndRemark&authKey=9db6f578d99c441a9ea0508e985052d6&maintenanceId=816&driverName=测试驾驶员&remark=测试数据
    //http://192.168.1.11:8095/vm/Maintenance.ashx?platform=SmartFam&action=GetListWorkContentsNormal&authKey=9db6f578d99c441a9ea0508e985052d6&maintenanceId=816
    public static final String ERROR_NET="500";
    public static final String SUCCESS="200";
    public static final String ERROR_USER="T0001";
    public static final String ERROR_VERSION="201";
    public static final String NO_USER="0002";
//    private static final int DEFAULT_TIMEOUT = 5;//超时时间
//    private LoginService loginService;
//    private Retrofit retrofit;

//    private HttpClient(){
//        //手动创建一个OkHttpClient并设置超时时间
//        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
//
//        retrofit=new Retrofit.Builder().client(httpClientBuilder.build()).addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .baseUrl(BASE_URL)
//                .build();
//        loginService=retrofit.create(LoginService.class);
//    }
//
//    //在访问HttpMethods时创建单例
//    private static class SingletonHolder{
//        private static final HttpClient INSTANCE = new HttpClient();
//    }
//
//    //获取单例
//    public static HttpClient getInstance(){
//        return HttpClient.SingletonHolder.INSTANCE;
//    }

    /**
     * 向指定url发送get请求
     *
     * @param url
     *            发送请求请求url
     * @param params
     *            请求参数
     * @return
     */
    public static String sendGet(String url, String params) {
        String result = "";
        BufferedReader br = null;
        try {
            String urlname = url;
            URL realUrl = new URL(urlname);
            // 打开和URL之间的链接
            URLConnection conn = realUrl.openConnection();
            // 建立实际的连接
            conn.connect();

            br = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                result += "\n" + line;
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {// 使用finally来关闭输入流
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 向指定url发送post请求
     *
     * @param url
     *            发送请求请求url
     * @param params
     *            请求参数
     * @return
     */
    public static String sendPost(String url, String params) {

        String result = "";

        PrintWriter out = null;
        BufferedReader in = null;

        try {
            URL realUrl = new URL(url);

            URLConnection conn = realUrl.openConnection();
            // 发送post请求必须设置以下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(params);
            // flush输出流的缓冲
            out.flush();
            // 读取url响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return result;
    }
}
